package com.aw.guiguzi.module.manager.transaction.adapter

import android.view.View
import android.widget.EditText
import com.aw.baselib.bean.CommonImgItemBean
import com.aw.baselib.util.AwEditTextListener
import com.aw.guiguzi.R
import com.aw.guiguzi.util.AppCacheUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

class ManagerTransactionConfigSubItemAdapter :
    BaseQuickAdapter<CommonImgItemBean, BaseViewHolder>(R.layout.adapter_manager_transaction_config_subitem) {
    private var mList: MutableList<CommonImgItemBean>? = ArrayList()
    private var mUploadListener: OnUploadListener? = null
    private var mOnDelListener: OnDelListener? = null

    init {
        if (mList == null) {
            mList = ArrayList()
        }
    }

    fun setUploadListener(uploadListener: OnUploadListener) {
        mUploadListener = uploadListener
    }

    fun setOnDelListener(onDelListener: OnDelListener?) {
        mOnDelListener = onDelListener
    }

    override fun convert(helper: BaseViewHolder, bean: CommonImgItemBean) {
        helper.setText(R.id.tv_desc, bean.describe)
            .setText(R.id.tv_url, bean.imgPath)
            .setText(R.id.tv_subTitile, "子项" + (helper.layoutPosition + 1))
//            .setGone(
//                R.id.tv_del,
//                helper.layoutPosition + 1 == mList!!.size && AppCacheUtil.isTransactionConfigDel()
//            )
            .addOnClickListener(R.id.tv_upload)
        helper.getView<View>(R.id.tv_upload).setOnClickListener { v: View? ->
            if (mUploadListener != null) {
                mUploadListener!!.toUpload(bean)
            }
        }
        helper.getView<View>(R.id.tv_del).setOnClickListener { v: View? ->
            if (mOnDelListener != null) {
                mOnDelListener!!.toDel(helper.layoutPosition)
            }
        }
        AwEditTextListener.isChangeListener(helper.getView(R.id.tv_desc)) { changed: Boolean ->
            if (changed) {
                bean.describe = (helper.getView<View>(R.id.tv_desc) as EditText).text.toString()
            }
        }
    }

    fun addAllData(dataList: MutableList<CommonImgItemBean>?) {
        mList = dataList
        setNewData(mList)
        notifyDataSetChanged()
    }

    fun clearData() {
        if (mList != null) {
            val startPosition = 0 //hasHeader is 1
            val preSize = mList!!.size
            if (preSize > 0) {
                mList!!.clear()
                notifyItemRangeRemoved(startPosition, preSize)
            }
        }
    }

    interface OnUploadListener {
        fun toUpload(bean: CommonImgItemBean?)
    }

    interface OnDelListener {
        fun toDel(index: Int)
    }
}